package com.brsya.movie.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.brsya.base.base.Contents;
import com.brsya.base.util.LoggerUtil;
import com.brsya.base.widget.CenterDialog;
import com.brsya.movie.R;
import com.brsya.movie.application.MyApplication;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdLoadType;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/7/26  21:11
 * Description:
 * 退出弹框
 */
public class ExitDialog extends CenterDialog implements View.OnClickListener {
    private final Context context;
    private final View rootView;
    private RelativeLayout rlAdBox;
    private TextView textCancel;
    private TextView textConfirm;

    private OnClickBtnListener onClickBtnListener;


    public ExitDialog(Context context) {
        super(context);
        this.context = context;

        setContentView(rootView = LayoutInflater.from(context).inflate(R.layout.dialog_exit, null, false));

        initView();
    }

    private void initView() {

        rlAdBox = (RelativeLayout) rootView.findViewById(R.id.rl_ad_box);
        textCancel = (TextView) rootView.findViewById(R.id.text_cancel);
        textConfirm = (TextView) rootView.findViewById(R.id.text_confirm);


        textCancel.setOnClickListener(this);
        textConfirm.setOnClickListener(this);


        loadAd();
    }

    /**
     * 加载广告
     */
    private void loadAd() {
        TTAdNative mTTAdNative = TTAdSdk.getAdManager().createAdNative(context);
        float width = context.getApplicationContext().getResources().getDisplayMetrics().xdpi;
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(Contents.AD_TT_SDK_EXIT_DIALOG) //广告位id
                .setSupportDeepLink(true)
                .setAdCount(1) //请求广告数量为1
                .setExpressViewAcceptedSize(270, 180) //期望模板广告view的size,单位dp
                .setAdLoadType(TTAdLoadType.LOAD)//推荐使用，用于标注此次的广告请求用途为预加载（当做缓存）还是实时加载，方便后续为开发者优化相关策略
                .build();

        mTTAdNative.loadBannerExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            //请求失败回调
            @Override
            public void onError(int code, String message) {
                LoggerUtil.e("banner广告加载失败" + code + "--ss--" + message);
            }

            //请求成功回调
            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                LoggerUtil.e("banner广告加载成功");
                for (TTNativeExpressAd adBean : ads) {
                    View adView = adBean.getExpressAdView();
                    if (rlAdBox != null) {
                        rlAdBox.addView(adView);
                        LoggerUtil.e("banner广告添加成功");
                    }
                    adBean.render();
                    break;
                }

            }
        });
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.text_cancel) {
            dismiss();
            if (onClickBtnListener != null) {
                onClickBtnListener.onDismiss();
            }
        } else if (id == R.id.text_confirm) {
            MyApplication.isAgree();
            dismiss();
            if (onClickBtnListener != null) {
                onClickBtnListener.onConfirm();
            }
        }
    }

    public void setOnClickBtnListener(OnClickBtnListener onClickBtnListener) {
        this.onClickBtnListener = onClickBtnListener;
    }

    public interface OnClickBtnListener {
        void onDismiss();

        void onConfirm();
    }
}
